********************************************************************************
*
**# Table 6: Who is Convicted in DV Court?
*
********************************************************************************

	* 	Part (1): DV Case 		-- Convicted 		-- Panel A - Left Panel
	* 	Part (2): Other Case 	-- Convicted 		-- Panel B - Left Panel
	* 	Part (3): DV Case 		-- Not Convicted 	-- Panel A - Right Panel
	* 	Part (4): Other Case 	-- Not Convicted 	-- Panel B - Right Panel
	

cap erase "${OUTPUT}/tab6.xlsx"

use "${DATA}/data_DV.dta", clear


********************************************************************************
*
**##	 (1)
*
********************************************************************************

# d ; 

keep if guilty_case == 1 ;

matrix drop _all ;


local charac " white age days_arrest_court charge_count prev_court 
			 prev_court_num prev_court_dv prev_court_aslt  " ;

local m = 4				;
local clab  = `m' - 1 	;	/* column label */
local pnl 	= `m' - 2 	; 	/* panel */
local ttl 	= `m' - 3 	; 	/* title */


putexcel set "${OUTPUT}/tab6", sheet("GS1_DV0") replace	 ;

	qui putexcel A`clab' = "Defendant Characteristics"			;
	qui putexcel B`clab' = "Uncod Mean Convicted in GS"			;
	qui putexcel C`clab' = "Convicted in DV Predicted Value" 	;
	qui putexcel D`clab' = "p-value" 							;

foreach x of local charac { ;					 
	
	local lhs_lbl : variable label `x' 			 		;	
	
	reghdfe `x' dv_judge if dv_case == 1, $hdfe_opts	;
	
		matrix B 	=r(table) 					;
		local beta	=round(B[1,1], 0.001) 		;
		local pval	=string(B[4,1], "%9.2f") 	;	
		
	ttest  `x' if e(sample)==1, by(dv_judge)	;
	
		local mean0	=round(r(mu_1), 0.001)			; 
		local pred 	=round(`mean0' + `beta',0.001)  ;

		local general_N =r(N_1) 			;
		local dv_N 		=r(N_2) 			;
		local total_N 	=`general_N'+`dv_N' ;	
		
	reghdfe dv_judge `charac'  if dv_case == 1, $hdfe_opts ;
	
		test `charac' 							;
		local ftest	=string(`r(F)', "%9.2f")	; 
		local fpval	=string(`r(p)', "%9.2f")	;
		
	putexcel set "${OUTPUT}/tab6", sheet("GS1_DV0") modify ;
	
		qui putexcel A`m' 	= "`lhs_lbl'" 	;
		qui putexcel B`m' 	= `mean0' 		;
		qui putexcel C`m' 	= `pred'		;
		qui putexcel D`m' 	= "(`pval')"	;
		qui putexcel A`pnl'  = "Panel A: DV Cases 		[ N = `total_N' ]" ;
		qui putexcel A`ttl'  = "Convicted " ;

local m `++m' 	;
dis `m' 		;

} ;

		qui putexcel A`m' = "N"			;
		qui putexcel B`m' = `general_N'	;
		qui putexcel C`m' = `dv_N'		;
		
		local m `++m' ;
		
		qui putexcel A`m' = "F-test of joint significance (p-value)"	;
		qui putexcel D`m' = "`ftest' (`fpval')"							;
		
# d cr 

********************************************************************************
*
**##	(2)
*
********************************************************************************

# d ; 

matrix drop _all 	;

local m = `m'+4 		;
local clab  = `m' - 1 	;	/* column label */
local pnl 	= `m' - 2 	; 	/* panel */


putexcel set "${OUTPUT}/tab6", sheet("GS1_DV0") modify	 		;
	qui putexcel A`clab' = "Defendant Characteristics"			;
	qui putexcel B`clab' = "Uncod Mean Convicted in GS"			;
	qui putexcel C`clab' = "Convicted in DV Predicted Value" 	;
	qui putexcel D`clab' = "p-value" 							;
	
foreach x of local charac { ;					 
	
	local lhs_lbl : variable label `x' 			 		;	
	
	reghdfe `x' dv_judge if dv_case == 0, $hdfe_opts 	;
	
		matrix B 	=r(table) 					;
		local beta	=round(B[1,1], 0.001)		;
		local pval	=string(B[4,1], "%9.2f") 	;	
		
	ttest  `x' if e(sample)==1, by(dv_judge)	;
	
		local mean0	=round(r(mu_1), 0.001)			; 
		local pred 	=round(`mean0' + `beta',0.001)  ;
		local general_N =r(N_1) 					;
		local dv_N 		=r(N_2) 					;
		local total_N 	=`general_N'+`dv_N' 		;	
		
	reghdfe dv_judge `charac'  if dv_case == 0, $hdfe_opts ;
	
		test `charac' 							;
		local ftest	=string(`r(F)', "%9.2f")	; 
		local fpval	=string(`r(p)', "%9.2f")	;
		
	putexcel set "${OUTPUT}/tab6", sheet("GS1_DV0") modify ;
	
		qui putexcel A`m' 	= "`lhs_lbl'" 		;
		qui putexcel B`m' 	= `mean0'			;
		qui putexcel C`m' 	= `pred'			;
		qui putexcel D`m'	  	= "(`pval')"	;
		qui putexcel A`pnl' 	= "Panel B: Other Cases 		[ n = `total_N' ]" ;


local m `++m' ;

} ;

		qui putexcel A`m' = "N"			;
		qui putexcel B`m' = `general_N'	;
		qui putexcel C`m' = `dv_N'		;

		local m `++m' ;
		
		qui putexcel A`m' = "F-test of joint significance (p-value)" 	;
		qui putexcel D`m' = "`ftest' (`fpval')"							;
		
		
#  d cr

********************************************************************************
*
**##	(3)
*
********************************************************************************

use "${DATA}/data_DV.dta", clear

keep if guilty_case == 0 

# d ;

matrix drop _all ;


local charac " white age days_arrest_court charge_count prev_court 
			 prev_court_num prev_court_dv prev_court_aslt  " ;

local m = 4	;

local clab  = `m' - 1 ;	/* column label */ 
local pnl 	= `m' - 2 ; /* panel 		*/
local ttl 	= `m' - 3 ; /* title 		*/


putexcel set "${OUTPUT}/tab6", sheet("GS1_DV0") modify	 			;

	qui putexcel F`clab' = "Defendant Characteristics"				;
	qui putexcel G`clab' = "Uncod Mean Not Convicted in GS"			;
	qui putexcel H`clab' = "Not Convicted in DV Predicted Value" 	;
	qui putexcel I`clab' = "p-value" 								;

foreach x of local charac { 	;					 
	
	local lhs_lbl : variable label `x'  ;	
	
	reghdfe `x' dv_judge if dv_case == 1, $hdfe_opts ;
	
		matrix B 	=r(table) 					;
		local beta	=round(B[1,1], 0.001) 		;	
		local pval	=string(B[4,1], "%9.2f") 	;	
		
	ttest  `x' if e(sample)==1, by(dv_judge)		;
	
		local mean0	=round(r(mu_1), 0.001)			; 
		local pred 	=round(`mean0' + `beta',0.001)  ;
		local general_N =r(N_1) 					;
		local dv_N 		=r(N_2) 					;
		local total_N 	=`general_N'+`dv_N' 		;	
		
	reghdfe dv_judge `charac'  if dv_case == 1, $hdfe_opts ;
	
		test `charac' 							;
		local ftest	=string(`r(F)', "%9.2f")	; 
		local fpval	=string(`r(p)', "%9.2f")	;
		
	putexcel set "${OUTPUT}/tab6", sheet("GS1_DV0") modify ;
	
		qui putexcel F`m' 	= "`lhs_lbl'" 	;
		qui putexcel G`m' 	= `mean0'		;
		qui putexcel H`m' 	= `pred'		;
		qui putexcel I`m' 	= "(`pval')"	;
		qui putexcel F`pnl'  = "Panel A: DV Cases 		[ N = `total_N' ]" ;
		qui putexcel F`ttl'  = " Not convicted " ;

local m `++m' 	;
dis `m' 		;

} ;

		qui putexcel F`m' = "N"			;
		qui putexcel G`m' = `general_N'	;
		qui putexcel H`m' = `dv_N'		;
		
		local m `++m' ;
		
		qui putexcel F`m' = "F-test of joint significance (p-value)"	;
		qui putexcel I`m' = "`ftest' (`fpval')"							;

# d cr 

********************************************************************************
*
**## (4)
*
********************************************************************************

# d ; 

matrix drop _all	;
local m = `m'+4 	;

local clab  = `m' - 1 ;	/* column label */
local pnl 	= `m' - 2 ; /* panel */ 


putexcel set "${OUTPUT}/tab6", sheet("GS1_DV0") modify	 			;

	qui putexcel F`clab' = "Defendant Characteristics"				;
	qui putexcel G`clab' = "Uncod Mean Not Convicted in GS"			;
	qui putexcel H`clab' = "Not Convicted in DV Predicted Value" 	;
	qui putexcel I`clab' = "p-value" 								;
	
foreach x of local charac { 	;					 
	
	local lhs_lbl : variable label `x' 					;	
	
	reghdfe `x' dv_judge if dv_case == 0, $hdfe_opts 	;
	
		matrix B 	=r(table) 							;
		local beta	=round(B[1,1], 0.001) 				;
		local pval	=string(B[4,1], "%9.2f") 			;	
		
	ttest  `x' if e(sample)==1, by(dv_judge)		;
	
		local mean0	=round(r(mu_1), 0.001)			; 
		local pred 	=round(`mean0' + `beta',0.001)  ;
		local general_N =r(N_1) 					;
		local dv_N 		=r(N_2) 					;
		local total_N 	=`general_N'+`dv_N' 		;	
		
	reghdfe dv_judge `charac'  if dv_case == 0, $hdfe_opts ;
	
		test `charac' 							;
		local ftest	=string(`r(F)', "%9.2f")	; 
		local fpval	=string(`r(p)', "%9.2f")	;
		
	putexcel set "${OUTPUT}/tab6", sheet("GS1_DV0") modify ;

		qui putexcel F`m' 	= "`lhs_lbl'" 		;
		qui putexcel G`m' 	= `mean0'			;
		qui putexcel H`m' 	= `pred'			;
		qui putexcel I`m'	  	= "(`pval')"	;
		qui putexcel F`pnl' 	= "Panel B: Other Cases 		[ n = `total_N' ]" ;


local m `++m' ;
} ;


		qui putexcel F`m' = "N"			;
		qui putexcel G`m' = `general_N'	;
		qui putexcel H`m' = `dv_N'		;

		local m `++m' ;
		
		qui putexcel F`m' = "F-test of joint significance (p-value)"	;
		qui putexcel I`m' = "`ftest' (`fpval')"	;
		
	

# d cr

********************************************************************************
*   End of File
********************************************************************************
